package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.entity.Namespace;
import com.repository.NamespaceRepository;
import com.service.NamespaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.slf4j.*;

import javax.naming.Name;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/ns")

public class NamespaceController {
    private static final Logger logger = LoggerFactory.getLogger(com.controller.NamespaceController.class);
    @Autowired
    NamespaceRepository namespaceRepository;

    @Autowired
    NamespaceService namespaceService;

    @GetMapping("/index")
    public String NamespaceDashboard(Model model) {
        List<Namespace> nslist = namespaceService.findAll();
        model.addAttribute("nslist",nslist);
        return "namespaces_list";
    }

    @ResponseBody
    @GetMapping("/list")
    public String NamespaceList(@RequestParam Integer page, @RequestParam(required=false) Namespace namespace,Integer size, @RequestParam(value="orderBy",defaultValue="id") String orderBy, @RequestParam(value="sortOrder",defaultValue="asc") String sortOrder){

       System.out.print("page:" + page + "  size:"+ size + "  orderBy:" + orderBy + "  sortOrder:" + sortOrder + "param" + namespace);

       Page<Namespace> ns = namespaceService.findByPageAndParams(page,size, namespace, orderBy, sortOrder);
        JSONObject result = new JSONObject();
        result.put("page",ns.getSize());
        result.put("total",ns.getTotalElements());
        result.put("rows",ns.getContent());
        //result.put("row",nsrow);
        System.out.println(result.toJSONString());
        return result.toJSONString();
    }

//    public String RemoteCompare(@ModelAttribute Namespace namespace){
//
//        return result.to;
//    }

    @PostMapping("/add")
    public Namespace namespaceForm (@ModelAttribute Namespace namespace) {
         Namespace newNs = new Namespace();
         newNs.setId(namespace.getId());
         newNs.setSpacename(namespace.getSpacename());
         return namespaceRepository.save(newNs);
    }

    @RequestMapping(value = "/tok8s", method = {RequestMethod.POST })
    @ResponseBody
    public String Query(@RequestBody Namespace[] nslist){
        List nsdifflist = namespaceService.findK8sNamespace(nslist);
        JSONObject result = new JSONObject();;
        result.put("nsdifflist",nsdifflist.toString());
        return result.toString();
    }
}
